//
//  MenuListTableViewCell2.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/8/29.
//  Copyright © 2020 moon. All rights reserved.
//

#import "MenuListTableViewCell2.h"
#import "MenuCollectionViewCellModel.h"
#import "MenuCollectionViewCell.h"
#import "TestUICollectionViewTitleSupplementaryView.h"
@implementation MenuListTableViewCell2
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.iconImageView fixedSize:CGSizeMake(30, 30)];
        [self.contentView addSubview:self.iconImageView];
        //
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.numberOfLines = 0;
        [self.contentView addSubview:self.titleLabel];

        self.expendButton = [[MKUIFlowLayoutButton alloc] init];
        [self.expendButton setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
        [self.expendButton setTitle:@"^" forState:UIControlStateNormal];
        [self.expendButton setTitle:@"v" forState:UIControlStateSelected];
        [self.expendButton addTarget:self action:@selector(__onExpendButtonChange:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.expendButton];
        MKUILayoutConstraintItemWrapper *w3 = [MKUILayoutConstraintItemWrapper wrapItem:self.expendButton fixedSize:CGSizeMake(44, 44)];
        //
        self.childflowlayout = [[[self.class childflowlayoutClass] alloc] init];
//        self.childflowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.childflowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        if(self.class.emptySpacing){
            self.childflowlayout.minimumInteritemSpacing = 0;
            self.childflowlayout.minimumLineSpacing = 0;
        }else{
            self.childflowlayout.minimumInteritemSpacing = 2;
            self.childflowlayout.minimumLineSpacing = 20;
        }
        if([self.class emptySpacing]){
            self.childflowlayout.sectionInset = UIEdgeInsetsZero;
        }else{
            self.childflowlayout.sectionInset = UIEdgeInsetsMake(5, 1, 10, 20);
        }
//        self.childflowlayout.sectionInset = UIEdgeInsetsZero;

        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.childflowlayout];
        if([self.class emptySpacing]){
            self.collectionView.contentInset = UIEdgeInsetsZero;
        }else{
            self.collectionView.contentInset = UIEdgeInsetsMake(5, 10, 15, 20);
        }
        self.collectionView.backgroundColor = [UIColor clearColor];
        self.collectionView.layer.borderWidth = 1;
        self.collectionView.layer.borderColor = [UIColor grayColor].CGColor;
        self.collectionView.bounces = NO;
        [self.contentView addSubview:self.collectionView];
        MKUILayoutConstraintItemWrapper *w4 = [MKUILayoutConstraintItemWrapper wrapItem:self.collectionView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            UICollectionView *collectionView = wrapper.originItem;
            size.height = 200;//用于横向滚动时，限制高度最大值
            CGSize s = [collectionView.mk_collectionViewFlowLayout mk_sizeThatFits:size];
            return s;
        }];
        self.collectionWrapper = w4;
        //
        self.collectionModel = [[MKUICollectionViewModel alloc] initWithCollectionView:self.collectionView];
        
        //
        MKUIFlowLayoutConstraint *flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[w1,self.titleLabel,w3] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_L) contentInsets:UIEdgeInsetsZero interitemSpacing:10];
        
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[flowlayout,w4] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_L) contentInsets:UIEdgeInsetsMake(10, 20, 10, 20) interitemSpacing:10];
        //
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
+ (Class)childflowlayoutClass{
    return [UICollectionViewFlowLayout class];
}
+ (BOOL)emptySpacing{
    return NO;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (void)__onExpendButtonChange:(id)sender{
    MenuListTableViewCellModel *cm = self.menuListCellModel;
    cm.expendChildren = !cm.expendChildren;
    [cm refreshWithAnimated:YES];
}

- (MenuListTableViewCellModel *)menuListCellModel{
    return (MenuListTableViewCellModel *)self.customCellModel;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    Menu *modelObject = self.customCellModel.modelObject;
    self.iconImageView.image = modelObject.icon;
    self.titleLabel.text = modelObject.title;
    
    MenuListTableViewCellModel *cm = self.menuListCellModel;
    self.expendButton.selected = cm.expendChildren;
    self.collectionView.hidden = !cm.expendChildren;
    if(cm.expendChildren){
        [self __reloadChildrenData];
    }
}
+ (Class)menuCellClass{
    return [MenuCollectionViewCell class];
}
- (void)__reloadChildrenData{
    [self reloadData];
}
- (void)reloadData{
    [self.collectionModel removeAllSectionModels];
    
    NSInteger count = 1;
    if(self.collectionView.mk_collectionViewFlowLayout.scrollDirection==UICollectionViewScrollDirectionVertical){
        count = 2;
    }
    for (int i=0; i<count; i++) {
        MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
        sm.showHead = YES;
        sm.headClass = [TestUICollectionViewTitleSupplementaryView class];
        if(self.collectionView.mk_collectionViewFlowLayout.scrollDirection==UICollectionViewScrollDirectionVertical){
            sm.headTitle = @"这是一个非常长的Head！！！这是一个非常长的Head！！！这是一个非常长的Head！！！";
        }else{
            sm.headTitle = @"这是一个非常长的Head！！！";
        }
        
        sm.showFoot = YES;
        sm.footClass = [TestUICollectionViewTitleSupplementaryView class];
        sm.footTitle = @"Foot";
        
        [self.collectionModel addSectionModel:sm];
        
        for (Menu *menu in self.menuListCellModel.childrenMenus) {
            MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
            cm.cellClass = [self.class menuCellClass];
            if(i==1||count<=1){
                [self.collectionModel addCellModel:cm];
            }
        }
    }
    [self.collectionModel reloadCollectionViewData];
}
- (CGSize)customSizeThatFits:(CGSize)size{//动态高度
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
@end

@implementation MenuListTableViewCell3

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.childflowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.collectionModel.forwardDelegate = self;
    }
    return self;
}

- (void)reloadData{
    [self.collectionModel removeAllSectionModels];

    NSInteger count = 1;
    if(self.collectionView.mk_collectionViewFlowLayout.scrollDirection==UICollectionViewScrollDirectionVertical){
        count = 2;
    }
    {
        MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
        [self.collectionModel addSectionModel:sm];
    }
    for (int i=0; i<count; i++) {
        MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
        sm.showHead = YES;
        sm.headClass = [TestUICollectionViewTitleSupplementaryView class];
        if(self.collectionView.mk_collectionViewFlowLayout.scrollDirection==UICollectionViewScrollDirectionVertical){
            sm.headTitle = @"这是一个非常长的Head！！！这是一个非常长的Head！！！这是一个非常长的Head！！！";
        }else{
            sm.headTitle = @"这是一个非常长的Head！！！这是一个非常长的Head";
        }

        sm.showFoot = YES;
        sm.footClass = [TestUICollectionViewTitleSupplementaryView class];
        sm.footTitle = @"Foot";

        [self.collectionModel addSectionModel:sm];

        for (Menu *menu in self.menuListCellModel.childrenMenus) {
            MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
            cm.cellClass = [self.class menuCellClass];
            [self.collectionModel addCellModel:cm];
        }
    }
    [self.collectionModel reloadCollectionViewData];
}
#pragma mark delegate:UICollectionViewDelegateFlowLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if(section%2==0){
        return 5;
    }else{
        return 0;
    }
}
@end
@implementation MenuListTableViewCell3_fix
+ (Class)childflowlayoutClass{
    return [MKUICollectionViewFixInteritemSpacingFlowLayout class];
}
@end
@implementation MenuListTableViewCell4
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.childflowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return self;
}
+ (Class)menuCellClass{
    return [MenuCollectionViewCell2 class];
}
@end


@implementation MenuListTableViewCell5
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.childflowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return self;
}
+ (Class)menuCellClass{
    return [MenuCollectionViewCell2 class];
}
@end

@implementation MenuListTableViewCell6
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.childflowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return self;
}
- (void)reloadData{
    [self.collectionModel removeAllSectionModels];
    
    for (int i=0; i<1; i++) {
        MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
        sm.showHead = YES;
        sm.headClass = [TestUICollectionViewTitleSupplementaryView class];
        sm.headTitle = @"Head";
        
        sm.showFoot = YES;
        sm.footClass = [TestUICollectionViewTitleSupplementaryView class];
        sm.footTitle = @"Foot";
        
        [self.collectionModel addSectionModel:sm];
        
        for (Menu *menu in self.menuListCellModel.childrenMenus) {
            MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
            cm.cellClass = [self.class menuCellClass];
            [self.collectionModel addCellModel:cm];
        }
    }
    [self.collectionModel reloadCollectionViewData];
}
+ (Class)menuCellClass{
    return [MenuCollectionViewCell class];
}
@end


@implementation MenuListTableViewCell7
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.childflowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView.contentInset = UIEdgeInsetsZero;
        self.collectionView.bounces = YES;
//        self.collectionView.pagingEnabled = YES;
        self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
        self.childflowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 30, 0);
        self.childflowlayout.minimumLineSpacing = 0;
        self.childflowlayout.minimumInteritemSpacing = 0;
        self.collectionModel.forwardDelegate = self;
        self.collectionWrapper.sizeThatFitsBlock = ^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            UICollectionView *collectionView = wrapper.originItem;
            size.height = 99999999;//用于横向滚动时，限制高度最大值
            CGSize s = [collectionView.mk_collectionViewFlowLayout mk_sizeThatFits:size];
            return s;
        };
        self.pageControl = [[UIPageControl alloc] init];
        [self.contentView addSubview:self.pageControl];
        [self colorTraitCollectionDidChange];
    }
    return self;
}
- (void)colorTraitCollectionDidChange{
    self.pageControl.pageIndicatorTintColor = [UIColor mk_colorWithLight:[UIColor grayColor]];
    self.pageControl.currentPageIndicatorTintColor = [UIColor systemBlueColor];
    self.collectionView.backgroundColor = [UIColor mk_colorWithLight:[UIColor whiteColor]];
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    CGRect f1 = bounds;
    f1.size = [self.pageControl sizeThatFits:bounds.size];
    f1.origin.y = CGRectGetMaxY(bounds)-f1.size.height;
    f1.origin.x = CGRectGetMinX(bounds)+(bounds.size.width-f1.size.width)/2;
    self.pageControl.frame = f1;
}
- (void)reloadData{
    [self.collectionModel removeAllSectionModels];
    
    {
        //每个cell一个分组，用于分页
        MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
        [self.collectionModel addSectionModel:sm];
        Menu *menu = [[Menu alloc] init];
        menu.title = @"这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！";
        MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
        cm.cellClass = [self.class menuCellClass];
        [self.collectionModel addCellModel:cm];
    }
    for (Menu *menu in self.menuListCellModel.childrenMenus) {
        //每个cell一个分组，用于分页
        MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
        [self.collectionModel addSectionModel:sm];
        
        MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
        cm.cellClass = [self.class menuCellClass];
        [self.collectionModel addCellModel:cm];
    }
    [self.collectionModel reloadCollectionViewData];
    self.pageControl.numberOfPages = self.collectionModel.numberOfCells;
}
- (void)tableView:(UITableView *)tableView willDisplayCellModel:(__kindof MKUITableViewCellModel *)cellModel{
    NSIndexPath *p = self.customCellModel.userInfo;
    if(p){
        [self.collectionView scrollToItemAtIndexPath:p atScrollPosition:(UICollectionViewScrollPositionCenteredHorizontally) animated:NO];
        
    }else{
//        [self.collectionView setContentOffset:CGPointMake(100, 0) animated:YES];
    }
    self.pageControl.currentPage = p.section;
}
+ (Class)menuCellClass{
    return [MenuCollectionViewCell3 class];
}
- (void)dealloc{
    self.collectionView.delegate = nil;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self __scrollviewDidEndScrolling:scrollView];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    if(scrollView.isTracking||scrollView.isDragging){
        [self __scrollviewDidEndScrolling:scrollView];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}
- (void)__scrollviewDidEndScrolling:(UIScrollView *)scrollView{
    NSIndexPath *p = [self __currentIndexPath];
    self.customCellModel.userInfo = p;
    self.pageControl.currentPage = p.section;
}
- (NSIndexPath *)__currentIndexPath{
    NSArray<NSIndexPath *> *indexpaths = self.collectionView.indexPathsForVisibleItems;
    CGFloat area = -1;
    NSIndexPath *indexpath = nil;
    CGRect bounds = self.collectionView.bounds;
    for (NSIndexPath *p in indexpaths) {
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:p];
        if(cell){
            CGRect f = cell.frame;
            CGRect r = CGRectIntersection(bounds, f);
            CGFloat a1 = r.size.width*r.size.height;
            if(a1>area){
                area = a1;
                indexpath = p;
            }
        }
    }
    return indexpath;
}

@end


@implementation MenuListTableViewCell8
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.collectionModel.forwardDelegate = self;
        self.childPageFlowlayout = [[MKUICollectionViewPagePickerFlowLayout alloc] init];
//        self.childPageFlowlayout = [[MKUICollectionViewPageFlowLayout alloc] init];
        
        self.collectionView.collectionViewLayout = self.childPageFlowlayout;
        
        self.childPageFlowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//        self.collectionView.contentInset = UIEdgeInsetsMake(2, 10, 6, 8);
        self.collectionView.contentInset = UIEdgeInsetsZero;
        self.collectionView.bounces = YES;
//        self.collectionView.pagingEnabled = YES;
        
        self.childPageFlowlayout.highlightPagingCell = YES;
        self.childPageFlowlayout.pagingEnabled = YES;
        self.childPageFlowlayout.enableCycleScroll = YES;
        self.childPageFlowlayout.itemSize = CGSizeMake(100, 60);
        self.childPageFlowlayout.pagingBoundsPosition = 0.5;
        self.childPageFlowlayout.pagingCellPosition = 0.5;
//        self.childPageFlowlayout.sectionInset = UIEdgeInsetsMake(5, 10, 30, 15);

//        self.childPageFlowlayout.interitemSpacing = 30;
        
        self.collectionWrapper.sizeThatFitsBlock = ^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
//            UICollectionView *collectionView = wrapper.originItem;
            size.height = 100;
            return size;
        };
        self.pageControl = [[UIPageControl alloc] init];
        [self.contentView addSubview:self.pageControl];
        //
        self.paingLine = [[UIView alloc] init];
        self.paingLine.backgroundColor = [UIColor systemBlueColor];
        [self.contentView addSubview:self.paingLine];
        //
        [self colorTraitCollectionDidChange];
    }
    return self;
}
- (void)colorTraitCollectionDidChange{
    self.pageControl.pageIndicatorTintColor = [UIColor mk_colorWithLight:[UIColor grayColor]];
    self.pageControl.currentPageIndicatorTintColor = [UIColor systemBlueColor];
    self.collectionView.backgroundColor = [UIColor mk_colorWithLight:[UIColor whiteColor]];
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    CGRect f1 = bounds;
    f1.size = [self.pageControl sizeThatFits:bounds.size];
    f1.origin.y = CGRectGetMaxY(bounds)-f1.size.height;
    f1.origin.x = CGRectGetMinX(bounds)+(bounds.size.width-f1.size.width)/2;
    self.pageControl.frame = f1;
    {
        CGRect f1 = bounds;
        f1.size.width = 1;
        f1.origin.x = self.collectionView.mk_collectionViewPageFlowLayout.pagingBoundsPosition*self.collectionView.bounds.size.width;
        f1.origin.x += self.collectionView.frame.origin.x;
        self.paingLine.frame = f1;
    }
}
- (void)reloadData{
    [self.collectionModel removeAllSectionModels];
    {
        //每个cell一个分组，用于分页
        MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
        [self.collectionModel addSectionModel:sm];
        Menu *menu = [[Menu alloc] init];
        menu.title = @"这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！这是一个非常长的菜单，应该要换行显示！！";
        MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
        cm.cellClass = [self.class menuCellClass];
        [self.collectionModel addCellModel:cm];
    }
    
    {
        //每个cell一个分组，用于分页
        MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
        [self.collectionModel addSectionModel:sm];
    }
    
    //每个cell一个分组，用于分页
    MKUICollectionViewTitleSupplementarySectionModel *sm = [[MKUICollectionViewTitleSupplementarySectionModel alloc] init];
    [self.collectionModel addSectionModel:sm];
    
    for (Menu *menu in [self.menuListCellModel.childrenMenus subarrayWithRange:NSMakeRange(0, 6)]) {
        
        MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
        cm.cellClass = [self.class menuCellClass];
        [self.collectionModel addCellModel:cm];
    }
    [self.collectionModel reloadCollectionViewData];
    self.pageControl.numberOfPages = self.collectionModel.numberOfCells;
}
- (void)tableView:(UITableView *)tableView willDisplayCellModel:(__kindof MKUITableViewCellModel *)cellModel{
    NSIndexPath *p = self.customCellModel.userInfo;
    if(p){
        [self.childPageFlowlayout setIndexPathAtPagingCell:p animated:NO];
    }else{
        p = self.childPageFlowlayout.indexPathAtPagingCell;
        self.customCellModel.userInfo = p;
    }
    self.pageControl.currentPage = p.section;
}
+ (Class)menuCellClass{
    return [MenuCollectionViewCell4 class];
}
- (void)dealloc{
//    self.collectionView.delegate = nil;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self __scrollviewDidEndScrolling:scrollView];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self __scrollviewDidEndScrolling:scrollView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    MKUICollectionViewCellModel *cm = [self.collectionModel cellModelAtIndexPath:self.collectionView.mk_collectionViewPageFlowLayout.indexPathAtPagingCell];
    if(cm){
        NSInteger index = [self.collectionModel.allCellModels indexOfObject:cm];
        self.pageControl.numberOfPages = self.collectionModel.numberOfCells;
        self.pageControl.currentPage = index;
    }
}
- (void)__scrollviewDidEndScrolling:(UIScrollView *)scrollView{
//    NSIndexPath *p = [self __currentIndexPath];
//    self.customCellModel.userInfo = p;
//    self.pageControl.currentPage = p.section;
}
- (NSIndexPath *)__currentIndexPath{
    NSArray<NSIndexPath *> *indexpaths = self.collectionView.indexPathsForVisibleItems;
    CGFloat area = -1;
    NSIndexPath *indexpath = nil;
    CGRect bounds = self.collectionView.bounds;
    for (NSIndexPath *p in indexpaths) {
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:p];
        if(cell){
            CGRect f = cell.frame;
            CGRect r = CGRectIntersection(bounds, f);
            CGFloat a1 = r.size.width*r.size.height;
            if(a1>area){
                area = a1;
                indexpath = p;
            }
        }
    }
    return indexpath;
}
#pragma mark - delegate:MKUICollectionViewDelegatePageFlowLayout
//- (CGSize)collectionView:(UICollectionView *)collectionView pageFlowLayout:(MKUICollectionViewPageFlowLayout *)collectionViewLayout itemSizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    MKUICollectionViewCellModel *collectionCellModel = [self.collectionModel cellModelAtIndexPath:indexPath];
//    return [MenuCollectionViewCell3 dynamicSizeWithCollectionView:collectionView collectionCellModel:collectionCellModel cellShareInstance:[MenuCollectionViewCell3 sharedInstance] calBlock:^CGSize(UICollectionView *collectionView, MKUICollectionViewCellModel *cellModel, MenuCollectionViewCell *cell) {
//        CGSize size = [cell.menuButton sizeThatFits:cell.bounds.size];
////        CGSize size = [cell.menuButton mk_sizeThatFits:cell.bounds.size limitInSize:CGSizeMake(100, 60)];
//        return size;
//    }];
//}
- (void)collectionView:(UICollectionView *)collectionView pageFlowLayout:(MKUICollectionViewPageFlowLayout *)collectionViewLayout didScrollToPagingCell:(NSIndexPath *)indexPathAtPagingCell{
    MKUICollectionViewCellModel *cm = [self.collectionModel cellModelAtIndexPath:indexPathAtPagingCell];
    NSInteger index = [self.collectionModel.allCellModels indexOfObject:cm];
    self.pageControl.numberOfPages = self.collectionModel.numberOfCells;
    self.pageControl.currentPage = index;
    self.cellModel.userInfo = indexPathAtPagingCell;
}
@end
